#include "World.h"
#include "DarkGDK.h"
#include "stdio.h"
#define debug 1


World::World( int WorldID, int Width, int Height, int XTiles, int YTiles, int MaxHeight ) // Function thats call when a World is Created. ie World Landscape(vars); would set landscape to the world object
{
	dbMakeMatrix( WorldID,  Width,  Height, XTiles, YTiles );
	dbRandomizeMatrix ( WorldID, MaxHeight );
	dbPositionMatrix ( WorldID, 0,0,0 );
	dbMakeLight ( 5 );
	dbSetSpotLight ( 5,5000, 50000 );
	dbPositionLight ( 5, 5000,1500,5000 );
	dbPointLight (5, 5000, 150, 5000 );
	dbColorLight ( 5, 252,188,0 );
	dbSetLightRange ( 5, 2000 );
	dbSetAmbientLight ( 100 );
	dbColorAmbientLight(dbRGB( 255,255,255)); 
	dbShowLight ( 5 ); 
    dbLoadObject("os.x",80000);
	//dbScaleObject(80000,5000,5000,5000);
	dbPositionObject(80000,0,0,0);
 dbSetCameraRange ( 0, 10000);
}

World::~World(){} // Deconstructor not used!

void World::PrepareSkyBox(char* Filename, int FreeObjectID )
{
   dbLoadObject("sky10.x", FreeObjectID);
   //dbScaleObject(5, 1000000,20000,1000000 );
   dbPositionObject(FreeObjectID,0,0,0);
  
}
void World::PrepareBaseTexture( int ImageNumber, int Across, int Down )
{
     dbPrepareMatrixTexture ( 1,  ImageNumber,  Across, Down );
}

void World::TextureFillAll( int TextureTile )
{
    dbFillMatrix( 1,200, TextureTile);
}

int World::GetWorldID(void)
{
	return WorldID;
}
int World::GetWidth(void)
{
	return Width;
}
int World::GetHeight(void)
{
    return Height;
}
int World::GetXTiles(void)
{
	return XTiles;
}
int World::GetYTiles(void)
{
    return YTiles;
}
int World::GetMaxHeight(void)
{
	return MaxHeight;
}

int World::GetXPlayerTile(int PlayerX )
{
   XPlayerTile = PlayerX/100;
   return XPlayerTile;
}

int World::GetZPlayerTile(int PlayerZ )
{
   ZPlayerTile = PlayerZ/100;
   return ZPlayerTile;
}

void World::SetTerrainTypeForTile(int TileX , int TileZ,  int Type)
 {
	if( Type == 1 )
	{
		dbSetMatrixTile( 1, TileX, TileZ, 1 );
	}
	if( Type == 2 )
	{
	    dbSetMatrixTile( 1, TileX, TileZ, 2 );
	}
}

    struct Tile
	{
	 int resourceID;
	 int house;
	 int ModelID;
	 int ResourceAmount;
	 unsigned char NameString;

	};

    Tile MasterResource[100][100];
    int randx = 0;
	int randz = 0;
    int i = 10;
    
	void World::GenerateResourceTree(int TotalAmount, int StartingID )
	{
		if(debug == 1)
		{
		if(dbFileExist("ResourceData.txt"))
		{
        dbDeleteFile("ResourceData.txt");
		}
		dbOpenToWrite ( 1, "ResourceData.txt" );
		}

			
       while(  i <= TotalAmount+StartingID )
	   {

		    randx = dbRND(100);
			randz = dbRND(100);

			MasterResource[randx][randz].resourceID = 1;
			MasterResource[randx][randz].ResourceAmount = dbRND(5);
			MasterResource[randx][randz].ModelID = i;
			dbMakeObjectCylinder ( i, 20 );
			dbPositionObject(i,(randx*100)+dbRND(50),dbGetMatrixHeight(1, randx*100, randz*100)+dbRND(100),(randz*100)+dbRND(50)); // Center of Tile
            dbScaleObject(i,40,2000,40);
			dbTextureObject(i,2);
			if (debug == 1)
			{
		    int resid = MasterResource[randx][randz].resourceID;
			dbWriteString ( 1, "TileX/Z" );
	        dbWriteString ( 1, "ResourceID: ");
			dbWriteString ( 1, dbStr(randx)); 
			dbWriteString( 1, dbStr(randz)); 
	        dbWriteString ( 1, dbStr(resid)); 
	        
			}
			i++;
           
	   }
	  
	  
	}

	void World::GenerateResourceIron(int TotalAmount, int StartingID )
	{
		if(debug == 1)
		{
		if(dbFileExist("ResourceData1.txt"))
		{
        dbDeleteFile("ResourceData1.txt");
		}
		dbOpenToWrite ( 2, "ResourceData1.txt" );
		}
     
		i = StartingID;
		dbLoadImage("ironore.jpg",3000);
			
       while(  i <= TotalAmount+StartingID )
	   {

		    randx = dbRND(100);
			randz = dbRND(100);

			MasterResource[randx][randz].resourceID = 2;
			MasterResource[randx][randz].ResourceAmount = 1;
			MasterResource[randx][randz].ModelID = i;
			dbMakeObjectBox(i,20,20,20);
			dbPositionObject(i,(randx*100)+(50),dbGetMatrixHeight(1, randx*100, randz*100)+150,(randz*100)+(50)); // Center of Tile
           // dbScaleObject(i,40,2000,40);
			dbTextureObject(i,3000);
			if (debug == 1)
			{
		    int resid = MasterResource[randx][randz].resourceID;
			dbWriteString ( 2, "TileX/Z" );
	        dbWriteString ( 2, "ResourceID: ");
			dbWriteString ( 2, dbStr(randx)); 
			dbWriteString( 2, dbStr(randz)); 
	        dbWriteString ( 2, dbStr(resid)); 
	        
			}
			i++;
           
	   }
	  
	  
	}


	int World::IsResourceAtTile(int X ,int Z)
	{
		if( MasterResource[X][Z].resourceID != 0)
		{
			return MasterResource[X][Z].resourceID;
		}
	}

	void World::DeleteResource(int X , int Z)
	{
		if(dbObjectExist(MasterResource[X][Z].ModelID))
		{
          dbDeleteObject(MasterResource[X][Z].ModelID);
		}
          
		MasterResource[X][Z].resourceID = 0;
	}

	int World::GetResourceAmountAtTile(int X, int Z)
	{
        return MasterResource[X][Z].ResourceAmount;
	}
    int TreeGrowth = 2500000;
	int World::ProcessTreeGrowth(void)
	{

            randx = dbRND(100);
			randz = dbRND(100);
		    randx = randx+dbRND(3);
			randz = randz+dbRND(3);
			if ( MasterResource[randx][randz].resourceID == 0 )
			{
		    int OldCamx = dbCameraPositionX();
		    int OldCamy = dbCameraPositionY();
		    int OldCamz = dbCameraPositionZ();
            MasterResource[randx][randz].resourceID = 1;
			MasterResource[randx][randz].ResourceAmount = dbRND(5);
			MasterResource[randx][randz].ModelID = TreeGrowth;
			dbMakeObjectCylinder ( TreeGrowth, 20 );
			dbPositionObject(TreeGrowth,(randx*100)+dbRND(50),dbGetMatrixHeight(1, randx*100, randz*100)+dbRND(100),(randz*100)+dbRND(50)); // Center of Tile
            dbScaleObject(TreeGrowth,40,2000,40);
			dbTextureObject(TreeGrowth,2);
			dbPositionCamera(OldCamx,OldCamy,OldCamz);
			TreeGrowth++;
			return 1;
			} 
			else
			{
				return 0;
			}
}